package com.itcast.mallapplet.service.impl;

import com.itcast.applet.dto.RiderAmendDTO;
import com.itcast.applet.dto.RiderDTO;
import com.itcast.applet.dto.RiderLoginDTO;
import com.itcast.applet.dto.RiderOrderDTO;
import com.itcast.applet.entity.Order;
import com.itcast.applet.entity.Rider;
import com.itcast.applet.entity.RiderTask;
import com.itcast.applet.vo.RiderTaskVO;
import com.itcast.exception.LoginFailedException;
import com.itcast.mallapplet.mapper.OrderMapper;
import com.itcast.mallapplet.mapper.RiderMapper;
import com.itcast.mallapplet.service.RiderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.time.LocalDateTime;
import java.util.List;

import static com.itcast.constant.MessageConstant.LOGIN_FAILED;

@Service
public class RiderServiceImpl implements RiderService {

    @Autowired
    private RiderMapper riderMapper;
    @Autowired
    private OrderMapper orderMapper;


    //骑手登录
    public Rider riderLogin(RiderLoginDTO riderLoginDTO) {
        Rider rider = riderMapper.getByAaccount(riderLoginDTO.getAccount(),riderLoginDTO.getPassword());
        if (rider == null) {
           throw new LoginFailedException(LOGIN_FAILED);
        }
        return rider;
    }

    //骑手查看订单
    public RiderTaskVO[] riderQuery(Long id) {
        //骑手 - 查骑手任务表 . order - order订单表    可一条sql查，也可分开查
        RiderTaskVO[] riderTaskVO = riderMapper.getByOrder(id);
        return riderTaskVO;
    }

    //骑手经纬度修改
    public void riderAmend(RiderAmendDTO riderAmendDTO) {
       //修改骑手经纬度
        riderMapper.riderAmendIng(riderAmendDTO);

    }

    //骑手完成订单
    @Transactional
    public void riderOrder(RiderOrderDTO riderOrderDTO) {
        Order order = orderMapper.orderGetById(riderOrderDTO.getOrderId());
        //订单完成
        orderMapper.riderOrder(order.getId());
        //骑手状态-空闲
        riderMapper.riderStatus(riderOrderDTO.getRiderId());
    }


}
